﻿//******************************************************************************************************
//  GatewayStatistics.cs - Gbtc
//
//  Copyright © 2010, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  03/09/2012 - Stephen C. Wills
//       Generated original version of source code.
//
//******************************************************************************************************

using System.Linq;
using TimeSeriesFramework.Transport;

namespace TimeSeriesFramework.Statistics
{
    internal static class GatewayStatistics
    {
        #region [ Subscriber Statistics ]

        private static double GetSubscriberStatistic_Connected(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = subscriber.IsConnected ? 1.0D : 0.0D;

            return statistic;
        }

        private static double GetSubscriberStatistic_Authenticated(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = subscriber.Authenticated ? 1.0D : 0.0D;

            return statistic;
        }

        private static double GetSubscriberStatistic_ProcessedMeasurements(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = s_statisticValueCache.GetDifference(subscriber, subscriber.ProcessedMeasurements, "ProcessedMeasurements");

            return statistic;
        }

        private static double GetSubscriberStatistic_TotalBytesReceived(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = s_statisticValueCache.GetDifference(subscriber, subscriber.TotalBytesReceived, "TotalBytesReceived");

            return statistic;
        }

        private static double GetSubscriberStatistic_AuthorizedCount(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = subscriber.GetAuthorizedSignalIDs().Count();

            return statistic;
        }

        private static double GetSubscriberStatistic_UnauthorizedCount(object source, string arguments)
        {
            double statistic = 0.0D;
            DataSubscriber subscriber = source as DataSubscriber;

            if ((object)subscriber != null)
                statistic = subscriber.GetUnauthorizedSignalIDs().Count();

            return statistic;
        }

        #endregion

        #region [ Publisher Statistics ]

        private static double GetPublisherStatistic_Connected(object source, string arguments)
        {
            double statistic = 0.0D;
            DataPublisher publisher = source as DataPublisher;

            if ((object)publisher != null)
                statistic = publisher.IsConnected ? 1.0D : 0.0D;

            return statistic;
        }

        private static double GetPublisherStatistic_ConnectedClientCount(object source, string arguments)
        {
            double statistic = 0.0D;
            DataPublisher publisher = source as DataPublisher;

            if ((object)publisher != null)
                statistic = publisher.Count;

            return statistic;
        }

        private static double GetPublisherStatistic_ProcessedMeasurements(object source, string arguments)
        {
            double statistic = 0.0D;
            DataPublisher publisher = source as DataPublisher;

            if ((object)publisher != null)
                statistic = s_statisticValueCache.GetDifference(publisher, publisher.ProcessedMeasurements, "ProcessedMeasurements");

            return statistic;
        }

        #endregion

        private static readonly StatisticValueStateCache s_statisticValueCache = new StatisticValueStateCache();
    }
}
